package com.example.building_material_mannager.controller;

import com.example.building_material_mannager.entity.User;
import com.example.building_material_mannager.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/verifyUser")
    public String verifyUserByUtel(HttpServletRequest request, HttpServletResponse response, String uTel , String uPassword){
        if (uTel==null || uTel.equals("")){
            request.setAttribute("uTelError","电话不能为空");
        }else if(uTel.length()!=11){
            request.setAttribute("uTelError","电话长度为11位");
        }
        if (uPassword==null || uPassword.equals("")){
            request.setAttribute("uPasswordError","密码不能为空");
        }else if(uPassword.length()<3 || uPassword.length()>15){
            request.setAttribute("uPasswordError","密码长度为3~15");
        }
        if (request.getAttribute("uTelError")!=null || request.getAttribute("uPasswordError")!=null){
            return "login";
        }

        User findUser = null;
        try {
            findUser = userService.verifyUserByUtel(uTel);
            if (uPassword.equals(findUser.getUpassword())){
                request.getSession().setAttribute("uTel",findUser.getUtel());
                return "index";
            }
        } catch (Exception e) {
            request.setAttribute("errors","系统维护中");
        }

        return "exceptions";
    }

}
